package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.UserService;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.SysUser;
import com.itheima.service.UserService;
import com.itheima.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;


@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

	@Reference
	private UserService userService;


	/**
	 * 获取用户名
	 * @return
	 */
	@RequestMapping("/getUsername")
	public Result getUsername(HttpSession session){
		SecurityContext context = SecurityContextHolder.getContext();
		Authentication authentication = context.getAuthentication();
		Object principal = authentication.getPrincipal();
		String username = null;
		if(principal instanceof User){
			User u = (User) principal;
			username = u.getUsername();
		}
		log.debug("获取到的用户名为：" + username);
		return new Result(true, MessageConst.GET_USERNAME_SUCCESS, username);
	}

	/**
	 * 登录成功后访问的方法
	 * 	登录成功以后： pages/main.html
	 */
	@RequestMapping("/loginSuccess")
	public ModelAndView loginSuccess(){
		log.debug("登录成功!!");
		return new ModelAndView("redirect:http://localhost:83/pages/main.html");
	}


	@RequestMapping("/loginFail")
	public ModelAndView loginFail(){
		log.debug("登录失败!!");
		return new ModelAndView("redirect:http://localhost:83/login.html");
	}

	@RequestMapping("/add")
	public Result add(@RequestBody SysUser sysUser,Integer[] roleIds){
		log.debug("controller:{"+sysUser+"}");
		userService.add(sysUser,roleIds);
		log.debug("用户添加成功！！");
		return new Result(true, "新增用户成功！");
	}

	@RequestMapping("/findPage")
	public Result findPage(@RequestBody QueryPageBean queryPageBean){
		log.debug("controller{"+queryPageBean+"}");
		PageResult pageResult = userService.findPage(queryPageBean);
		log.debug("用户分页查询成功!!");
		return new Result(true, "分页查询成功！", pageResult);
	}

	@RequestMapping("/delById")
	public Result delById(Integer id){
		log.debug("SysUserController:delete: " + id);
		userService.delById(id);
		log.debug("用户删除成功!!");
		return new Result(true, "用户删除成功！！");
	}

	@RequestMapping("/findById")
	public Result findById(Integer id){
		log.debug("UserController：findById:" + id);
		SysUser sysUser = userService.findById(id);
		log.debug("根据id查询用户成功!!");
		return new Result(true, "根据id查询用户成功！",sysUser);
	}


	@RequestMapping("/findRoleIdsById")
	public Result findRoleIdsById(Integer id){
		log.debug("UserController:findRoleIdsById: " + id);
		List<Integer> roleIds = userService.findRoleIdsById(id);
		log.debug("根据用户id查询角色id成功!!");
		return new Result(true, "根据用户id查询角色id成功!!",roleIds);
	}


	@RequestMapping("/edit")
	public Result edit(@RequestBody SysUser sysUser,Integer[] roleIds){
		log.debug("UserController:edit: " + sysUser);
		userService.edit(sysUser,roleIds);
		log.debug("用户修改成功!!");
		return new Result(true, "用户修改成功！");
	}



	@PostMapping("/getUserMenuByUsername")
	public Result getUserMenuByUsername() {
		SecurityContext context = SecurityContextHolder.getContext();
		//获取认证对象
		Authentication authentication = context.getAuthentication();
		//获取principal 对象
		Object principal = authentication.getPrincipal();
		//如果 没有登录，获取到的是匿名用户，如果的登录了获取到的User对象
		//instanceof : 实例类型判断运算符
		String username = null;
		if(principal instanceof User){
			User u = (User) principal;
			username = u.getUsername();
		}
		log.debug("getUserMenuByUsername方法执行，参数是:" + username);
		Result result = userService.getUserMenuByUsername(username);
		log.debug("getUserMenuByUsername方法执行完成：" + result);
		return result;
	}
}